package com.example.abby.zyfypt331jj.activity;
/**
 * Created by Abby on 2017/5/3.
 */
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;

import com.example.abby.zyfypt331jj.R;
import com.example.abby.zyfypt331jj.bean.LoginBean;
import com.example.abby.zyfypt331jj.iface.LoginListener;
import com.example.abby.zyfypt331jj.model.LoginModel;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    private Button loginButton, registerButton;
    private EditText idET, pwdEt;
    private String username="",password="",sessionID="";
    private Switch sw;
    private SharedPreferences sp;
    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            if(loginBean.getId()!=null){
                sessionID=loginBean.getSessionid().toString();
                System.out.println("----sessionID="+sessionID);
                savaSp();
                Intent intent1 = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent1);
                //finish();
            }
        }

        @Override
        public void onFail(String msg) {
            System.out.println("fail");
        }
    };




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        sp=getSharedPreferences("login",MODE_PRIVATE);
        readSP();
    }
    private void savaSp() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putString("sessionID",sessionID);
        editor.putBoolean("remember",sw.isChecked());
        editor.commit();
    }
    private void readSP() {
        String name=sp.getString("username",null);
        String pass=sp.getString("password",null);
        //String sessionID=sp.getString("sessionID",null);
        Boolean b=sp.getBoolean("remember",false);
        System.out.println("----name="+name);
        if(b){
            idET.setText(name);
            pwdEt.setText(pass);
            sw.setChecked(b);
        }
    }

    private void init() {
        loginButton = (Button)findViewById(R.id.login_button);
        registerButton = (Button)findViewById(R.id.register_button);
        idET = (EditText)findViewById(R.id.login_user);
        pwdEt = (EditText)findViewById(R.id.login_password);

        sw=(Switch) findViewById(R.id.switch1);
        loginButton.setOnClickListener(this);
        registerButton.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_button:
                System.out.println("点击登陆按钮");
                username=idET.getText().toString();
                password=pwdEt.getText().toString();
                LoginModel loginModel = new LoginModel();

                loginModel.getResult(username, password, loginListener);
                //savaSp();
                break;
            case R.id.register_button:
                System.out.println("点击注册按钮");
                Intent intent = new Intent();
                intent.setClass(this, RegisterActivity.class);//forward为当前activity，forwardTarget为新的activity
                startActivity(intent);
                break;
        }
    }
}
